package com.ruoyi.device.controller;

import java.util.Base64;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.device.domain.Device;
import com.ruoyi.device.service.IDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
//import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.PubRequest;
import com.aliyuncs.iot.model.v20180120.PubResponse;
import com.aliyuncs.profile.DefaultProfile;
/**
 * deviceController
 * 
 * @author ruoyi
 * @date 2024-06-17
 */
@RestController
@RequestMapping("/device/device")
public class DeviceController extends BaseController
{
    boolean i = true;
    @Autowired
    private IDeviceService deviceService;
    DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI5tL3j6HHN1TkyoALiK6v","YQB74ng7LcXF0Tj5m55aT5vffHEapT");

    IAcsClient client = new DefaultAcsClient(profile);
    /**
     * 查询device列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出device列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:export')")
    @Log(title = "device", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "device数据");
    }

    /**
     * 获取device详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceService.selectDeviceById(id));
    }

    /**
     * 新增device
     */
    @PreAuthorize("@ss.hasPermi('device:device:add')")
    @Log(title = "device", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }




    /**
     * 修改device
     */
    @PreAuthorize("@ss.hasPermi('device:device:edit')")
    @Log(title = "device", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Device device)
    {
        return toAjax(deviceService.updateDevice(device));

//        sendDeviceStatus()
//        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除device
     */
    @PreAuthorize("@ss.hasPermi('device:device:remove')")
    @Log(title = "device", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }

    //阿里云接口
    @PostMapping("/aliyun")
    public void sendDeviceStatus(@RequestBody JSONObject msg){
        PubRequest request = new PubRequest();
        request.setIotInstanceId("iot-06z00jdf6gi3m3n");
        request.setProductKey("k0yrprh8fAd");
        request.setTopicFullName(String.format("/sys/%s/%s/thing/service/property/set",
              "k0yrprh8fAd","Num1"));

        JSONObject json = new JSONObject();
        json.put("id","230788029");
        json.put("method","thing.event.property.post");
        json.put("version","1.0");

        Base64.Encoder encoder = Base64.getEncoder();
        json.put("params",msg);
        request.setMessageContent(encoder.encodeToString(json.toJSONString().getBytes()));

        try {
            PubResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

}
